package com.thinkcube.edutainment.fragment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.MyLessonAdapter;
import com.thinkcube.edutainment.client.CourseClient;
import com.thinkcube.edutainment.client.LessonClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.Lesson;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.LessonData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.domain.dto.UserOAthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.mycourse)
public class MyCoureseDashbordLesson extends Fragment {

	@ViewById(R.id.mycourselist)
	ListView myCourseList;
	
	@ViewById(R.id.not_found)
	TextView notFound;

	@RestService
	CourseClient courseClient;

	@Bean
	CourseDto courseDto;

	@RestService
	UserClient userClient;

	@RestService
	LessonClient lessonClient;

	@Bean
	OAuthDto oAuthDto;

	@Trace
	@AfterViews
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setTokenUi(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	void setTokenUi(OAuth requestToken) {

		doBackground(requestToken);
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	UserOAthDto userOAthDto = new UserOAthDto();

	@Pref
	SPreferences_ pref;

	@Background
	void doBackground(OAuth requestToken) {

		userOAthDto.setAuthToken(requestToken.getoAuth().getToken());
		userOAthDto.setUserId(pref.getId().get());

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doUi(lessonClient.getUserLesson(userOAthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@Bean
	MyLessonAdapter myCourseAdapter;
	@ViewById(R.id.progressBar)
	ProgressBar progress;

	@Trace
	@UiThread
	void doUi(Lesson lesson) {

		if (lesson != null) {
			if (lesson.getLessonList() != null) {
				List<LessonData> lessonList = lesson.getLessonList();
				myCourseAdapter.setLessonDatList(lessonList);
				myCourseList.setAdapter(myCourseAdapter);
				
			}
			else
			{
				notFound.setVisibility(View.VISIBLE);
			}
		}
		
		progress.setVisibility(View.GONE);

	}

}
